<template>
    <div>
        <h1>科室新增</h1>
        <table class="table table-bordered" style="background-color:palegreen;">
            <tbody>
                 <tr>
                    <td>科室名称</td>
                    <td><input type="text" v-model="info.name"></td>
                 </tr>
                  <tr>
                    <td>选择科室</td>
                    <td>
                          <select v-model="info.oid">
            <option value="">请选择</option>
            <option :value="item.oid" v-for="item in level" :key="item.oid">{{item.oname}}</option>  
        </select>
                    </td>
                 </tr>
                  <tr>
                    <td>科室人数</td>
                    <td><input type="text" v-model="info.people"></td>
                 </tr>
                  <tr>
                    <td>科室描述</td>
                    <td><input type="text" v-model="info.introduce"></td>
                 </tr>
                  <tr>
                    <td>创建日期</td>
                    <td><input type="date" v-model="info.createTime"></td>
                 </tr>
                
                 <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="add"></td>
                 </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import {ref,onMounted} from 'vue'

onMounted(()=>{
    Getlevel();
})

let level:any=ref([]);

let info:any=ref({
  "id": 0,
  "people": "",
  "name": "",
  "introduce": "",
  "isdel": false,
  "createTime": "",
  "oid": ""
})

  const add=()=>{
    if(info.value.name==""){
        alert("科室名称不能为空");
        return;
     }
     if(info.value.people==""){
        alert("科室人数不能为空");
        return;
     }
     if(info.value.introduce==""){
        alert("科室描述不能为空");
        return;
     }
     axios({
        url:'https://localhost:7292/api/Office/AddOffice',
        method:'post',
        data:info.value
     })
     .then((res)=>{
        console.log(res);
        if(res.data==-1){
            alert("科室名称重复了")
            return;
        }
        alert("添加成功");
        location.href='/showOffcie'
     })
     .catch((err)=>{
        console.log(err);
     })
  }

  const Getlevel=()=>{
     axios({
        url:'https://localhost:7292/api/Office/GetLevelOffcie',
        method:'get',
     })
     .then((res)=>{
        console.log(res);
        level.value=res.data
     })
     .catch((err)=>{
        console.log(err);
     })
  }
</script>

<style scoped>

</style>